package chat.clients;

import chat.clients.fb.helper.FBMessageListener;
import design.patterns.state.State;
import org.jivesoftware.smack.*;
import org.jivesoftware.smack.packet.Presence;

import java.util.ArrayList;
import java.util.List;

import static org.jivesoftware.smack.packet.Presence.Type.unavailable;

public abstract class XmppClient extends State.Context {
    public static final int XMPP_DEFAULT_PORT = 5222;

    public static final State CONNECTING = new State("connecting");
    public static final State LOGGING_IN = new State("logging in");
    public static final State LOGGED_IN = new State("logged in");
    public static final State LOGGED_OUT = new State("logged out");

    public static final List<RosterEntry> rosterEntries = new ArrayList<>();

    protected XMPPConnection connection;
    protected String user;
    protected FBMessageListener messageListener;

    public final List<RosterEntry> getRosterEntries() {

        Roster roster = getRoster();

        System.out.println(roster);

        if (roster == null)
            return new ArrayList<>();

        for (RosterEntry entry : roster.getEntries()) {
            Presence presence = roster.getPresence(entry.getUser());
            if (presence != null && presence.getType() != unavailable) {
                rosterEntries.add(entry);
            } else
                System.out.println("-->" + entry + ", " + presence);
        }

        messageListener.setFriends(rosterEntries);

        return rosterEntries;
    }

    public abstract String connect() throws XMPPException;

    public final XmppClient login(String userName, String password)
            throws XMPPException {
        connect();

        State oldState = getState();

        setState(LOGGING_IN);

        if ((connection != null) && (connection.isConnected())) {
            connection.login(userName, password);
            user = connection.getUser();
            messageListener = new FBMessageListener(connection);
            setState(LOGGED_IN);
        } else
            setState(oldState);

        return this;
    }

    public final Chat createChat(String userJID, MessageListener listener) {
        return connection.getChatManager().createChat(userJID, listener);
    }

    public final Roster getRoster() {
        if (connection == null)
            return null;

        return connection.getRoster();
    }

    public final String getUser() {
        return user;
    }

    public final XmppClient disconnect() {
        connection.disconnect();

        if ((connection != null) && (connection.isConnected())) {
            Presence presence = new Presence(unavailable);
            presence.setStatus("offline");
            connection.disconnect(presence);
        }

        setState(LOGGED_OUT);

        return this;
    }

    public final void sendMessage(final RosterEntry entry, String text)
            throws XMPPException {
        if ((connection != null) && (connection.isConnected())) {
            ChatManager chatManager = connection.getChatManager();
            Chat chat = chatManager.createChat(entry.getUser(), messageListener);
            chat.sendMessage(text);
        }
    }
}
